# Replication code for:
# Winning at all costs? How negative partisanship affects voter decision-making
# Study 2

# packages ####
library(dplyr)
library(ggplot2)
library(cregg)
library(estimatr)
library(marginaleffects)
library(sjPlot)

# To replicate the analyses, set your working directory to the replication folder that contains the datasets. A default working directory is currently set to a folder called "POBE Replication Files_Strategic Voting" on your Desktop. 
setwd("~/Desktop/POBE Replication Files_Strategic Voting/")

# load data ####
dat <- read.csv("study2_data.csv")

# DATA CLEANING -------------- ####

# pass attention check to complete survey ####
dat <- subset(dat, attentioncheck1==3)

# recode pid3 including leaners ####
dat$pid3<-NA
dat$pid3[dat$pid==1 | dat$pid_ind==1] <- "Democrat"
dat$pid3[dat$pid==2 | dat$pid_ind==2] <- "Republican"
dat$pid3[dat$pid==3 & dat$pid_ind==3] <- "Independent"
dat$pid3[dat$pid==4 & dat$pid_ind==3] <- "Independent"
dat$pid3[dat$pid==5 & dat$pid_ind==3] <- "Independent"
dat$pid3 <- as.factor(dat$pid3)
table(dat$pid3)

# recode party win ####
dat$partywin_dems <- car::recode(dat$partywin_dems, "5=1; 6=2; 3=3; 2=4")
dat$partywin_repubs <- car::recode(dat$partywin_repubs, "5=1; 6=2; 3=3; 2=4")
dat$partywin_dems[dat$partywin_dems==""] <- NA
dat$partywin_repubs[dat$partywin_repubs==""] <- NA

dat <- dat %>%
  mutate(partywin = coalesce(partywin_repubs, partywin_dems))

dat$partywin_level <- NA
dat$partywin_level[dat$partywin==3 | dat$partywin==4] <- "Important"
dat$partywin_level[dat$partywin==1 | dat$partywin==2] <- "Not Important"
dat$partywin_level <- as.factor(dat$partywin_level)

# recode feeling thermomters ####
dat <- subset(dat, !is.na(dat$pid3))

dat$feelingtherm_outparty <- NA
dat$feelingtherm_outparty[dat$pid3=="Democrat"] <- dat$feelingtherm_repubs[dat$pid3=="Democrat"]
dat$feelingtherm_outparty[dat$pid3=="Republican"] <- dat$feelingtherm_dems[dat$pid3=="Republican"]


dat$feelingtherm_inparty <- NA
dat$feelingtherm_inparty[dat$pid3=="Democrat"] <- dat$feelingtherm_dems[dat$pid3=="Democrat"]
dat$feelingtherm_inparty[dat$pid3=="Republican"] <- dat$feelingtherm_repubs[dat$pid3=="Republican"]

# net partisan affect (inparty rating minus outparty rating)
dat$netaffect <- (dat$feelingtherm_inparty - dat$feelingtherm_outparty)


# create outparty level
out_cutoff <- quantile(dat$feelingtherm_outparty, probs = c(.10), na.rm=TRUE)[[1]]
# 10 percentile = 0 

dat$outparty_level <- NA
dat$outparty_level[dat$feelingtherm_outparty==out_cutoff] <- "High in NP"
dat$outparty_level[dat$feelingtherm_outparty > out_cutoff] <- "Everyone else"
dat$outparty_level<-as.factor(dat$outparty_level)

# create inparty level
in_cutoff <- quantile(dat$feelingtherm_inparty, probs = c(.90), na.rm=TRUE)[[1]]
# 90 percentile = 100

dat$inparty_level <- NA
dat$inparty_level[dat$feelingtherm_inparty==in_cutoff] <- "High in PP"
dat$inparty_level[dat$feelingtherm_inparty < in_cutoff] <- "Everyone else"
dat$inparty_level<-as.factor(dat$inparty_level)

# create netaffect level
netlow_cutoff <- quantile(dat$netaffect, probs = c(.10), na.rm=TRUE)[[1]]
nethigh_cutoff <- quantile(dat$netaffect, probs = c(.90), na.rm=TRUE)[[1]]

dat$netaffect_level <- NA
dat$netaffect_level[dat$netaffect>nethigh_cutoff] <- "High Net Neg Affect"
dat$netaffect_level[dat$netaffect < netlow_cutoff] <- "Low Net Neg Affect"
dat$netaffect_level<-as.factor(dat$netaffect_level)

# DESCRIPTIVES, CORRELATIONS --------------------- ####

# remove true independents ####
dat <- subset(dat, pid3!="Independent")

# Table C1 ####
prop.table(table(dat$ethnicity))
prop.table(table(dat$hispanic))
prop.table(table(dat$gender))
prop.table(table(dat$pid3))
prop.table(table(dat$education))

# Table 1 ####
summary(dat$feelingtherm_outparty)
sd(dat$feelingtherm_outparty, na.rm=TRUE)
summary(dat$feelingtherm_inparty)
sd(dat$feelingtherm_inparty, na.rm=TRUE)

# Table E1 ####
cor.test(dat$feelingtherm_outparty, dat$feelingtherm_inparty)
cor.test(dat$feelingtherm_outparty, dat$partywin)
cor.test(dat$feelingtherm_inparty, dat$partywin)

# SET UP CONJOINT DATA --------------- #### 

# combine dem and repub DVs ####
dat <-dat %>%
  mutate(vote1 = coalesce(vote_repub1, vote_dem1, vote_ind1),
         vote2 = coalesce(vote_repub2, vote_dem2, vote_ind2),
         vote3 = coalesce(vote_repub3, vote_dem3, vote_ind3),
         vote4 = coalesce(vote_repub4, vote_dem4, vote_ind4),
         sincere1 = coalesce(sincere_repub1, sincere_dem1, sincere_ind1),
         sincere2 = coalesce(sincere_repub2, sincere_dem2, sincere_ind2),
         sincere3 = coalesce(sincere_repub3, sincere_dem3, sincere_ind3),
         sincere4 = coalesce(sincere_repub4, sincere_dem4, sincere_ind4))

# create variable for number of issues congruent on ####

# Trial 1
dat$congruent1_1 <- NA
dat$congruent1_1[dat$issue1_1answer=="Oppose" & dat$support1_1=="Supports"] <- 0
dat$congruent1_1[dat$issue1_1answer=="Support" & dat$support1_1=="Opposes"] <- 0
dat$congruent1_1[dat$issue1_1answer=="Oppose" & dat$support1_1=="Opposes"] <- 1
dat$congruent1_1[dat$issue1_1answer=="Support" & dat$support1_1=="Supports"] <- 1

dat$congruent2_1 <- NA
dat$congruent2_1[dat$issue2_1answer=="Oppose" & dat$support2_1=="Supports"] <- 0
dat$congruent2_1[dat$issue2_1answer=="Support" & dat$support2_1=="Opposes"] <- 0
dat$congruent2_1[dat$issue2_1answer=="Oppose" & dat$support2_1=="Opposes"] <- 1
dat$congruent2_1[dat$issue2_1answer=="Support" & dat$support2_1=="Supports"] <- 1

dat$congruent_1 <- dat$congruent1_1 + dat$congruent2_1

# Trial 2
dat$congruent1_2 <- NA
dat$congruent1_2[dat$issue1_2answer=="Oppose" & dat$support1_2=="Supports"] <- 0
dat$congruent1_2[dat$issue1_2answer=="Support" & dat$support1_2=="Opposes"] <- 0
dat$congruent1_2[dat$issue1_2answer=="Oppose" & dat$support1_2=="Opposes"] <- 1
dat$congruent1_2[dat$issue1_2answer=="Support" & dat$support1_2=="Supports"] <- 1

dat$congruent2_2 <- NA
dat$congruent2_2[dat$issue2_2answer=="Oppose" & dat$support2_2=="Supports"] <- 0
dat$congruent2_2[dat$issue2_2answer=="Support" & dat$support2_2=="Opposes"] <- 0
dat$congruent2_2[dat$issue2_2answer=="Oppose" & dat$support2_2=="Opposes"] <- 1
dat$congruent2_2[dat$issue2_2answer=="Support" & dat$support2_2=="Supports"] <- 1

dat$congruent_2 <- dat$congruent1_2 + dat$congruent2_2

# Trial 3
dat$congruent1_3 <- NA
dat$congruent1_3[dat$issue1_3answer=="Oppose" & dat$support1_3=="Supports"] <- 0
dat$congruent1_3[dat$issue1_3answer=="Support" & dat$support1_3=="Opposes"] <- 0
dat$congruent1_3[dat$issue1_3answer=="Oppose" & dat$support1_3=="Opposes"] <- 1
dat$congruent1_3[dat$issue1_3answer=="Support" & dat$support1_3=="Supports"] <- 1

dat$congruent2_3 <- NA
dat$congruent2_3[dat$issue2_3answer=="Oppose" & dat$support2_3=="Supports"] <- 0
dat$congruent2_3[dat$issue2_3answer=="Support" & dat$support2_3=="Opposes"] <- 0
dat$congruent2_3[dat$issue2_3answer=="Oppose" & dat$support2_3=="Opposes"] <- 1
dat$congruent2_3[dat$issue2_3answer=="Support" & dat$support2_3=="Supports"] <- 1

dat$congruent_3 <- dat$congruent1_3 + dat$congruent2_3

# Trial 4
dat$congruent1_4 <- NA
dat$congruent1_4[dat$issue1_4answer=="Oppose" & dat$support1_4=="Supports"] <- 0
dat$congruent1_4[dat$issue1_4answer=="Support" & dat$support1_4=="Opposes"] <- 0
dat$congruent1_4[dat$issue1_4answer=="Oppose" & dat$support1_4=="Opposes"] <- 1
dat$congruent1_4[dat$issue1_4answer=="Support" & dat$support1_4=="Supports"] <- 1

dat$congruent2_4 <- NA
dat$congruent2_4[dat$issue2_4answer=="Oppose" & dat$support2_4=="Supports"] <- 0
dat$congruent2_4[dat$issue2_4answer=="Support" & dat$support2_4=="Opposes"] <- 0
dat$congruent2_4[dat$issue2_4answer=="Oppose" & dat$support2_4=="Opposes"] <- 1
dat$congruent2_4[dat$issue2_4answer=="Support" & dat$support2_4=="Supports"] <- 1

dat$congruent_4 <- dat$congruent1_4 + dat$congruent2_4

# stack conjoint trials ####
trial1 <- dplyr::select(dat, starts_with("feelingtherm"), "vote1", "sincere1", "win_1", "congruent_1", "attentioncheck2_1", "attentioncheck2_5", "outparty_level","inparty_level","netaffect_level", "partywin", "partywin_level", "pid3","gender", "education", "ideo7", "ResponseId")


trial1 <- dplyr::rename(trial1, "vote"="vote1",
                         "sincere"="sincere1",
                         "win"="win_1",
                         "congruent"="congruent_1")

trial1$vote[trial1$vote==2] <- 0
trial1$sincere[trial1$sincere==2] <- 0

trial2 <- dplyr::select(dat, starts_with("feelingtherm"), "vote2", "sincere2", "win_2", "congruent_2", "attentioncheck2_1", "attentioncheck2_5", "outparty_level","inparty_level","netaffect_level", "partywin", "partywin_level", "pid3","gender", "education", "ideo7", "ResponseId")


trial2 <- dplyr::rename(trial2, "vote"="vote2",
                        "sincere"="sincere2",
                        "win"="win_2",
                        "congruent"="congruent_2")

trial2$vote[trial2$vote==2] <- 0
trial2$sincere[trial2$sincere==2] <- 0

trial3 <- dplyr::select(dat, starts_with("feelingtherm"), "vote3", "sincere3", "win_3", "congruent_3", "attentioncheck2_1", "attentioncheck2_5", "outparty_level","inparty_level","netaffect_level", "partywin", "partywin_level", "pid3","gender", "education", "ideo7", "ResponseId")


trial3 <- dplyr::rename(trial3, "vote"="vote3",
                        "sincere"="sincere3",
                        "win"="win_3",
                        "congruent"="congruent_3")

trial3$vote[trial3$vote==2] <- 0
trial3$sincere[trial3$sincere==2] <- 0


trial4 <- dplyr::select(dat, starts_with("feelingtherm"), "vote4", "sincere4", "win_4", "congruent_4", "attentioncheck2_1", "attentioncheck2_5", "outparty_level","inparty_level","netaffect_level", "partywin", "partywin_level", "pid3","gender", "education", "ideo7", "ResponseId")


trial4 <- dplyr::rename(trial4, "vote"="vote4",
                        "sincere"="sincere4",
                        "win"="win_4",
                        "congruent"="congruent_4")

trial4$vote[trial4$vote==2] <- 0
trial4$sincere[trial4$sincere==2] <- 0


# stack
datstacked <- rbind(trial1, trial2, trial3, trial4)
datstacked <- droplevels(datstacked)


## recode electability
datstacked$win[datstacked$win=="Candidate A will likely WIN against"] <- "Win"
datstacked$win[datstacked$win=="it will likely be a TOSS UP between Candidate A and"] <- "Toss up"

datstacked$congruent <- as.factor(datstacked$congruent)
datstacked$win <- factor(datstacked$win, levels=c("Toss up", "Win"))

# RESULTS -------- #####

# the trade off ####
datstacked$mismatch <- NA
datstacked$mismatch <- (datstacked$vote - datstacked$sincere)

## % of time that respondents select the same candidate for strategic/sincere #
datstacked$tradeoff <- NA
datstacked$tradeoff[datstacked$mismatch==1] <- 1
datstacked$tradeoff[datstacked$mismatch<1] <- 0
prop.table(table(datstacked$tradeoff))

# Main effects ####

##  AMCES (Appendix H) ####

# vote choice (DV1):
amce <- cj(datstacked, vote ~ congruent + win, id = ~ ResponseId, feature_labels = list(congruent="Shared issue positiions", win="Chance of Winning"), estimate="amce")

amceplot1 <- plot(amce,
           feature_headers = FALSE,
           vline=0, vline_color = "grey30", 
           feature_labels=FALSE,
           size=2) +  
  ggplot2::facet_wrap(~feature, ncol = 1L,scales = "free_y",
                      strip.position = "top") + 
  xlab("Average Marginal Component Effect")+
  theme_minimal() +
  theme(legend.position="none", plot.title=element_text(size=12),
        axis.text=element_text(size=9),
        axis.title=element_text(size=9), 
        strip.text = element_text(size = 10)) +
  ggtitle("Vote for (strategic choice)")+
  scale_x_continuous(limits=c(-0.50, 0.50)) +
  scale_colour_manual(values=c("black", "black"))+
  scale_shape_manual(values=c(16, 16))+
  aes(shape = feature)

#png("amces_vote.png", height=4, width=4, units="in", res=350)
#amceplot1
#dev.off()

# sincere choice (DV2):
amce2 <- cj(datstacked, sincere ~ congruent + win, id = ~ ResponseId, feature_labels = list(congruent="Shared issue positiions", win="Chance of Winning"), estimate="amce")

amceplot2 <- plot(amce2,
           feature_headers = FALSE,
           vline=0, vline_color = "grey30", 
           feature_labels=FALSE,
           size=2) +  
  ggplot2::facet_wrap(~feature, ncol = 1L,scales = "free_y",
                      strip.position = "top") + 
  xlab("Average Marginal Component Effect")+
  theme_minimal() +
  theme(legend.position="none", plot.title=element_text(size=12),
        axis.text=element_text(size=9),
        axis.title=element_text(size=9), 
        strip.text = element_text(size = 10)) +
  ggtitle("Thinks represents best (sincere choice)")+
  scale_x_continuous(limits=c(-0.50, 0.53),breaks=c(-0.50, -0.25, 0.00, 0.25, 0.50)) +
  scale_colour_manual(values=c("black", "black"))+
  scale_shape_manual(values=c(16, 16))+
  aes(shape = feature)

#png("amces_sincere.png", height=4, width=4, units="in", res=350)
#amceplot2
#dev.off()

## MMs ####

# vote choice (DV1):
mm <- cj(datstacked, vote ~ congruent + win, id = ~ ResponseId, feature_labels = list(congruent="Shared issue positiions", win="Chance of Winning"), estimate="mm")

# Figure 1 ####
mmplot <- plot(mm,
           feature_headers = FALSE,
           vline=.5, vline_color = "grey30", 
           feature_labels=FALSE,
           size=2) +  
  ggplot2::facet_wrap(~feature, ncol = 1L,scales = "free_y",
                      strip.position = "top") + 
  xlab("Marginal Mean")+
  theme_minimal() +
  theme(legend.position="none", plot.title=element_text(size=12),
        axis.text=element_text(size=9),
        axis.title=element_text(size=9), 
        strip.text = element_text(size = 10)) +
  ggtitle("Vote for (strategic choice)")+
  scale_colour_manual(values=c("black", "black"))+
  scale_shape_manual(values=c(16, 16))+
  scale_x_continuous(limits=c(0,1))+
  aes(shape = feature)

#png("mm_vote.png", height=4, width=4, units="in", res=350)
#mmplot
#dev.off()

# Table F2 ####
mmtable <- subset(mm, select=c(outcome, feature, level, estimate, std.error, lower, upper))
print(xtable(mmtable), include.rownames=FALSE)

# Sincere choice (Appendix G) ####
mm2 <- cj(datstacked, sincere ~ congruent + win, id = ~ ResponseId, feature_labels = list(congruent="Shared issue positiions", win="Chance of Winning"), estimate="mm")

mmplot2 <- plot(mm2,
           feature_headers = FALSE,
           vline=.5, vline_color = "grey30", 
           feature_labels=FALSE,
           size=2) +  
  ggplot2::facet_wrap(~feature, ncol = 1L,scales = "free_y",
                      strip.position = "top") + 
  xlab("Marginal Mean")+
  theme_minimal() +
  theme(legend.position="none", plot.title=element_text(size=12),
        axis.text=element_text(size=9),
        axis.title=element_text(size=9), 
        strip.text = element_text(size = 10)) +
  ggtitle("Thinks represents best (sincere choice)")+
  scale_colour_manual(values=c("black", "black"))+
  scale_x_continuous(limits=c(0,1))+
  scale_shape_manual(values=c(16, 16))+
  aes(shape = feature)

#png("mm_sincere.png", height=4, width=4, units="in", res=350)
#mmplot2
#dev.off()

mmtable2 <- subset(mm2, select=c(outcome, feature, level, estimate, std.error, lower, upper))
print(xtable(mmtable2), include.rownames=FALSE)


# BY NEGATIVE PARTISANSHIP ------ ####

## MMs ####

## Figure 2 ####
# vote choice (DV1):
mm_neg <- cj(datstacked, vote ~ congruent + win, id = ~ ResponseId, feature_labels = list(congruent="Shared issue positiions", win="Chance of Winning"), estimate="mm", by=~outparty_level)

mm_negplot <- plot(mm_neg,
           feature_headers = FALSE,
           vline=.5, vline_color = "grey30", 
           feature_labels=FALSE,
           size=2) +  
  ggplot2::facet_wrap(~feature, ncol = 1L,scales = "free_y",
                      strip.position = "top") + 
  xlab("Marginal Mean")+
  theme_minimal() +
  theme(legend.position="bottom", plot.title=element_text(size=12),
        axis.text=element_text(size=9),legend.title=element_blank(),
        axis.title=element_text(size=9), 
        strip.text = element_text(size = 10)) +
  ggtitle("Vote for (strategic choice)")+
  scale_colour_manual(values=c("black", "black"))+
  scale_shape_manual(values=c(1, 16))+
  scale_x_continuous(limits=c(0,1))+
  guides(color = "legend")+
  aes(shape = BY)+
  aes(colour = BY)


#png("mm_vote_negparty.png", height=4, width=4, units="in", res=350)
#mm_negplot
#dev.off()

## Different thresholds (Appendix I)####

# 20%, 40%, 60%... 
datstacked$outparty_quants <- cut(datstacked$feelingtherm_outparty, breaks = quantile(datstacked$feelingtherm_outparty, probs = c(0, .20, .40, .60, .80, 1), na.rm=TRUE), include.lowest = TRUE, labels = c("0-20%", "21-40%", "41-60%", "61-80%", "81-100%"))

quantile(datstacked$feelingtherm_outparty, probs = c(0, .20, .40, .60, .80, 1), na.rm=TRUE)
quantile(datstacked$feelingtherm_outparty, probs = c(.21, .41, .61, .81), na.rm=TRUE)

# vote choice (DV1):
quants <- cj(datstacked, vote ~ congruent + win, id = ~ ResponseId, feature_labels = list(congruent="Shared issue positiions", win="Chance of Winning"), estimate="mm", by=~outparty_quants)


quantsplot <- plot(quants,
                    feature_headers = FALSE,
                    vline=.5, vline_color = "grey30", 
                    feature_labels=FALSE,
                    size=1) +  
  ggplot2::facet_wrap(~feature, ncol = 1L,scales = "free_y",
                      strip.position = "top") + 
  xlab("Marginal Mean")+
  theme_minimal() +
  theme(legend.position="bottom", plot.title=element_text(size=12),
        axis.text=element_text(size=9),legend.title=element_blank(),
        axis.title=element_text(size=9), 
        strip.text = element_text(size = 10)) +
  ggtitle("Vote for (strategic choice)")+
  scale_shape_manual(
    values = c(19, 18, 17, 15, 1),
    breaks = c("0-20%", "21-40%",  "41-60%",  "61-80%", "81-100%"),
    labels = c("0-20% \n(0-2)", "21-40% \n(2-10)",  "41-60% \n(10-26)",  "61-80% \n(27-49)", "81-100% \n(50-100)")) +
  scale_color_manual(
    values = c("#F8766D", "#B79F00", "#00BA38", "#00BFC4", "#619CFF"),
    breaks = c("0-20%", "21-40%",  "41-60%",  "61-80%", "81-100%"),
    labels = c("0-20% \n(0-2)", "21-40% \n(2-10)",  "41-60% \n(10-26)",  "61-80% \n(27-49)", "81-100% \n(50-100)")) +
  scale_x_continuous(limits=c(0, 1))+
  guides(color = "legend")+
  aes(shape = BY)+
  aes(colour = BY)
quantsplot

#png("mm_vote_negparty_quintiles.png", height=5, width=5, units="in", res=350)
#quantsplot
#dev.off()

## Table F4 ####
names(mm_neg)
mm_negtable <- subset(mm_neg, select=c(outparty_level, outcome, feature, level, estimate, std.error, lower, upper))
print(xtable(mm_negtable), include.rownames=FALSE)

## Sincere Choice (Appendix G) ####
mm_neg2 <- cj(datstacked, sincere ~ congruent + win, id = ~ ResponseId, feature_labels = list(congruent="Shared issue positiions", win="Chance of Winning"), estimate="mm", by=~outparty_level)

mm_neg2plot <- plot(mm_neg2,
           feature_headers = FALSE,
           vline=.5, vline_color = "grey30", 
           feature_labels=FALSE,
           size=2) +  
  ggplot2::facet_wrap(~feature, ncol = 1L,scales = "free_y",
                      strip.position = "top") + 
  xlab("Marginal Mean")+
  theme_minimal() +
  theme(legend.position="bottom", legend.title=element_blank(),
        plot.title=element_text(size=12),
        axis.text=element_text(size=9),
        axis.title=element_text(size=9), 
        strip.text = element_text(size = 10)) +
  ggtitle("Thinks represents best (sincere choice)")+
  scale_colour_manual(values=c("black", "black"))+
  scale_x_continuous(limits=c(0,1))+
  scale_shape_manual(values=c(1, 16))+
  guides(color = "legend")+
  aes(shape = BY)+
  aes(colour = BY)

#png("mm_sincere_negparty.png", height=4, width=4, units="in", res=350)
#mm_neg2plot
#dev.off()

# BY POSITIVE PARTISANSHIP ------ ####

## MMs ####
# vote choice (DV1):
mm_pos <- cj(datstacked, vote ~ congruent + win, id = ~ ResponseId, feature_labels = list(congruent="Shared issue positiions", win="Chance of Winning"), estimate="mm", by=~inparty_level)

## Figure 3 ####
mm_posplot <- plot(mm_pos,
                   feature_headers = FALSE,
                   vline=.5, vline_color = "grey30", 
                   feature_labels=FALSE,
                   size=2) +  
  ggplot2::facet_wrap(~feature, ncol = 1L,scales = "free_y",
                      strip.position = "top") + 
  xlab("Marginal Mean")+
  theme_minimal() +
  theme(legend.position="bottom", plot.title=element_text(size=12),
        axis.text=element_text(size=9),legend.title=element_blank(),
        axis.title=element_text(size=9), 
        strip.text = element_text(size = 10)) +
  ggtitle("Vote for (strategic choice)")+
  scale_colour_manual(values=c("black", "black"))+
  scale_shape_manual(values=c(1, 16))+
  scale_x_continuous(limits=c(0,1))+
  guides(color = "legend")+
  aes(shape = BY)+
  aes(colour = BY)


#png("mm_vote_posparty.png", height=4, width=4, units="in", res=350)
#mm_posplot
#dev.off()

# OTHER MEASURES ####

## net affect (Appendix K) ####

mm_net<- cj(datstacked, vote ~ congruent + win, id = ~ ResponseId, feature_labels = list(congruent="Shared issue positiions", win="Chance of Winning"), estimate="mm", by=~netaffect_level)

mm_netplot <- plot(mm_net,
                   feature_headers = FALSE,
                   vline=.5, vline_color = "grey30", 
                   feature_labels=FALSE,
                   size=2) +  
  ggplot2::facet_wrap(~feature, ncol = 1L,scales = "free_y",
                      strip.position = "top") + 
  xlab("Marginal Mean")+
  theme_minimal() +
  theme(legend.position="bottom", plot.title=element_text(size=12),
        axis.text=element_text(size=9),legend.title=element_blank(),
        axis.title=element_text(size=9), 
        strip.text = element_text(size = 10)) +
  ggtitle("Vote for (strategic choice)")+
  scale_colour_manual(values=c("black", "black"))+
  scale_shape_manual(values=c(16, 1))+
  scale_x_continuous(limits=c(0,1))+
  guides(color = "legend")+
  aes(shape = BY)+
  aes(colour = BY)


#png("mm_vote_netaffect.png", height=4, width=4, units="in", res=350)
#mm_netplot
#dev.off()


## party win (Appendix L) ####
mm_pw<- cj(datstacked, vote ~ congruent + win, id = ~ ResponseId, feature_labels = list(congruent="Shared issue positiions", win="Chance of Winning"), estimate="mm", by=~partywin_level)

mm_pwplot <- plot(mm_pw,
                feature_headers = FALSE,
                vline=.5, vline_color = "grey30", 
                feature_labels=FALSE,
                size=2) +  
  ggplot2::facet_wrap(~feature, ncol = 1L,scales = "free_y",
                      strip.position = "top") + 
  xlab("Marginal Mean")+
  theme_minimal() +
  theme(legend.position="bottom", 
        plot.title=element_text(size=12),
        legend.title=element_blank(),
        axis.text=element_text(size=9),
        axis.title=element_text(size=9), 
        strip.text = element_text(size = 10)) +
  ggtitle("Vote for (strategic choice)")+
  scale_x_continuous(limits=c(0,1)) +
  scale_colour_manual(values=c("black", "black"))+
  scale_shape_manual(values=c(16, 1))+
  guides(color = "legend")+
  aes(shape = BY)+
  aes(colour = BY)


#png("mm_vote_partywin.png", height=5, width=4.5, units="in", res=350)
#mm_pwplot
#dev.off()

# Marginal effects of toss up ----------- ####

model1 <- lm_robust(vote ~ win*feelingtherm_outparty*congruent, data = datstacked, cluster = ResponseId)

summary(model1)

tab_model(model1)
model1table <- model1 %>% tidy
model1table <- subset(model1table, select=c(term, estimate, std.error, p.value))
print(xtable(model1table), include.rownames=FALSE)

## Figure 4 ####
modelplot <- plot_cme(model1, variables="win", condition=c("feelingtherm_outparty"))+
  theme_minimal() +geom_hline(yintercept=0.0, linetype="dashed", color = "grey70")+
  labs(title = "Win vs. Toss up", y="Marginal effect of electability", x="Outparty Rating") +
  theme(legend.position="bottom", 
        plot.title=element_text(size=12),
        legend.title=element_blank(),
        axis.text.x = element_text(size=10),
        axis.text.y = element_text(size=10), 
        strip.text = element_text(size = 10)) +
  scale_y_continuous(limits=c(-0.07, 0.15))


#png("effectof_tossup_outparty_linear.png", height=4, width=4.5, units="in", res=350)
#modelplot
#dev.off()


## Partisan affiliation (Appendix M) ####

win_therm_pid <- lm_robust(vote ~ win*congruent*feelingtherm_outparty*pid3, data = datstacked, cluster = ResponseId)

plot <- plot_cme(win_therm_pid, variables="win", condition=c("feelingtherm_outparty", "pid3"))+
  theme_minimal() +
  labs(title = "Win vs. Toss up by PID", y="Marginal effect of electability", x="Outparty Rating") +
  theme(legend.position="bottom", 
        plot.title=element_text(size=12),
        legend.title=element_blank(),
        axis.text.x = element_text(size=10),
        axis.text.y = element_text(size=10), 
        strip.text = element_text(size = 10)) + scale_colour_manual(values=c("darkblue", "red")) + scale_fill_manual(values=c("dodgerblue", "pink"))
plot

#png("effectof_win_negparty_bypid_study2.png", height=4, width=4, units="in", res=350)
#plot
#dev.off()


# TRADE OFF ---------- ####
model <- lm_robust(vote ~ win*outparty_level*congruent, data = datstacked, cluster = ResponseId)

summary(model)

# Table of results (Appendix F) #
get_model_data(model, type = "pred", terms = c("congruent", "win", "outparty_level"))

model_table <- get_model_data(model, type = "pred", terms = c("congruent", "win", "outparty_level"))
names(model_table)
model_table <- subset(model_table, select=c(x, facet, group, predicted, std.error, conf.low, conf.high))
print(xtable(model_table), include.rownames=FALSE)

# Figure 5 ####
p <- plot_model(model, type = "pred", terms = c("congruent", "outparty_level", "win"), show.values=TRUE, 
                ci.lvl = .95, 
                colors=c("grey30", "black")) +
  geom_line() + theme_minimal()+geom_hline(yintercept=0.5, linetype="dashed", color = "grey70")+
  labs(title = "", y="Predicted vote", x="Issue Agreement \n (number of shared issue positions)") + 
  theme(legend.position="bottom", 
        plot.title=element_text(size=12),
        legend.title=element_blank(),
        axis.text.x = element_text(size=8),
        axis.text.y = element_text(size=8), 
        strip.text = element_text(size = 10)) +
  aes(shape = group) +
  aes(color = group) +
  scale_shape_manual(values = c(1, 16))+
  guides(color = "legend") +
  guides(shape = "legend") + coord_cartesian(ylim=c(0.2, 0.9))

#png("vote_negparty_flipped_study2.png", height=4, width=4.5, units="in", res=350)
#p
#dev.off()

## Different thresholds (Appendix I) ####
model <- lm_robust(vote ~ win*outparty_quants*congruent, data = datstacked, cluster = ResponseId)

p <- plot_model(model, type = "pred", terms = c("congruent", "outparty_quants", "win"), show.values=TRUE, 
                ci.lvl = .95, dodge = .8) + 
  theme_minimal()+geom_hline(yintercept=0.5, linetype="dashed", color = "grey70")+
  labs(title = "", y="Predicted vote", x="Issue Agreement \n (number of shared issue positions)") + 
  theme(legend.position="bottom", 
        plot.title=element_text(size=12),
        legend.title=element_blank(),
        axis.text.x = element_text(size=8),
        axis.text.y = element_text(size=8), 
        strip.text = element_text(size = 10)) +
  aes(shape = group) +
  aes(color = group) +
  scale_shape_manual(
    values = c(19, 18, 17, 15, 1),
    breaks = c("0-20%", "21-40%",  "41-60%",  "61-80%", "81-100%"),
    labels = c("0-20% \n(0-2)", "21-40% \n(2-10)",  "41-60% \n(10-26)",  "61-80% \n(27-49)", "81-100% \n(50-100)")) +
  scale_color_manual(
    values = c("#F8766D", "#B79F00", "#00BA38", "#00BFC4", "#619CFF"),
    breaks = c("0-20%", "21-40%",  "41-60%",  "61-80%", "81-100%"),
    labels = c("0-20% \n(0-2)", "21-40% \n(2-10)",  "41-60% \n(10-26)",  "61-80% \n(27-49)", "81-100% \n(50-100)")) +
  guides(color = "legend") +
  guides(shape = "legend") + coord_cartesian(ylim=c(0.2, 0.9))

p

#png("vote_negparty_flipped_study2_quintiles.png", height=5, width=4.5, units="in", res=350)
#p
#dev.off()

